#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int const Xenter = 10;
int const Yenter = 10;
CvPoint const ENTRANCE_LEFT_UP   = cvPoint ( 0,330 );
CvPoint const ENTRANCE_RIGHT_DOWN   = cvPoint ( 50 ,240 );

int car_counter;

static bool carArrived( CvPoint center, int lp )
{
	car_counter++;
	return true;		
}

uchar* copyData_to_arry(uchar *data,int height,int width,int channels,int step){
    uchar* new_data = new uchar[height*width*channels];
	for (int y=0; y < height; ++y) 
		for (int x=0; x < width; ++x) {
			new_data[y*step+x*channels] = data[y*step+x*channels+0];
			new_data[y*step+x*channels+1] = data[y*step+x*channels+1];
			new_data[y*step+x*channels+2] = data[y*step+x*channels+2] ;
	}
	return new_data;
}
int main (int argc, char **argv) {
	int height, width, step, channels;
	uchar *data;
	cvNamedWindow("TESTING checkIfCarEnter", 1);
	CvCapture* Capture = cvCaptureFromAVI("\\\\ibm134\\Public\\temp\\Osl_clips\\demo_big.avi");
	cvGrabFrame(Capture);
	IplImage *img =  cvRetrieveFrame(Capture);  // retrieve the captured frame
	height = img->height;
	width = img->width;
	step = img->widthStep;
	channels  = img->nChannels;
	data = (uchar *)img->imageData;
	uchar* first_frame_rgb = copyData_to_arry(data,height,width,channels,step);
	car_counter= 0;
	while ( cvGrabFrame(Capture) ) { 
		img =  cvRetrieveFrame(Capture); 
		data = (uchar *)img->imageData;
		checkIfCarEnter(data , first_frame_rgb,step,channels);
		cvShowImage("TESTING checkIfCarEnter", img);
		cvWaitKey(10);
	}
	if (car_counter == 2 ) cout << "PASS";
	else cout << "FAIL";
	int tmp;
	cin >> tmp;
	return 1;
}
